﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Tesla.Service.WcfServiceCommon;
using System.ServiceModel;
using Tesla.Service.ServiceContracts;
using Tesla.Service.DataContracts;
using Tesla.Util.ValidateEntity;


namespace Tesla.Web
{
    public class EmployeeServiceDispatcher
    {
        public IList<BaseDC> DisplayAllEmployee(int gridSize, ref int? totalRecordCount)
        {

            ISvcEmployee _proxySvcEmployee;
            ChannelFactory<ISvcEmployee> chfEmployee = null; IList<BaseDC> employees = null;

            try
            {
                chfEmployee = GADNChannelFactoryProvider.CreateChannelFactory<ISvcEmployee>();
                _proxySvcEmployee = chfEmployee.CreateChannel();
                employees = _proxySvcEmployee.DisplayAll(gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfEmployee.Close();
            }
            return employees;
        }

        

        public IList<BaseDC> SearchEmployeeByName(string name, int gridSize, ref int? totalRecordCount)
        {
            ISvcEmployee _proxySvcEmployee;
            ChannelFactory<ISvcEmployee> chfEmployee = null; IList<BaseDC> employees = null;

            try
            {
                chfEmployee = GADNChannelFactoryProvider.CreateChannelFactory<ISvcEmployee>();
                _proxySvcEmployee = chfEmployee.CreateChannel();
                employees = _proxySvcEmployee.SearchByName(name, gridSize, ref totalRecordCount);
            }
            catch (FaultException gfx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfEmployee.Close();
            }
            return employees;
        }

        public IList<BaseDC> ReadEmployee(int id)
        {
            ISvcEmployee _proxySvcEmployee;
            ChannelFactory<ISvcEmployee> chfEmployee = null; IList<BaseDC> employees = null;

            try
            {
                chfEmployee = GADNChannelFactoryProvider.CreateChannelFactory<ISvcEmployee>();
                _proxySvcEmployee = chfEmployee.CreateChannel();
                employees = _proxySvcEmployee.Read(id);
            }
            catch (FaultException gfx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfEmployee.Close();
            }
            return employees;
        }

        public IList<BaseDC> ReadEmployeeByRowNumberRange(int startRecordNo, int endRecordNo, string sortColName, string sortDirection, string searchString)
        {
            ISvcEmployee _proxySvcEmployee;
            ChannelFactory<ISvcEmployee> chfEmployee = null; IList<BaseDC> employees = null;

            try
            {
                chfEmployee = GADNChannelFactoryProvider.CreateChannelFactory<ISvcEmployee>();
                _proxySvcEmployee = chfEmployee.CreateChannel();
                employees = _proxySvcEmployee.ReadByRange(startRecordNo, endRecordNo, sortColName, sortDirection, searchString);
            }
            catch (FaultException gfx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (TimeoutException tx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfEmployee.Close();
            }
            return employees;
        }


        public int? CreateEmployee(BaseDC bdc)
        {
            ISvcEmployee _proxySvcEmployee;
            ChannelFactory<ISvcEmployee> chfEmployee = null; int? success = 0;

            try
            {
                chfEmployee = GADNChannelFactoryProvider.CreateChannelFactory<ISvcEmployee>();
                _proxySvcEmployee = chfEmployee.CreateChannel();
                success = _proxySvcEmployee.Create(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfEmployee.Close();
            }
            return success;
        }

        public int UpdateEmployee(BaseDC bdc)
        {
            ISvcEmployee _proxySvcEmployee;
            ChannelFactory<ISvcEmployee> chfEmployee = null; int updateResult = 0;

            try
            {
                chfEmployee = GADNChannelFactoryProvider.CreateChannelFactory<ISvcEmployee>();
                _proxySvcEmployee = chfEmployee.CreateChannel();
                updateResult = _proxySvcEmployee.Update(bdc);
            }
            catch (FaultException gfx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
                throw gfx;
            }
            catch (TimeoutException tx)
            {
                if (chfEmployee.State == CommunicationState.Faulted || chfEmployee.State == CommunicationState.Opened)
                    chfEmployee.Abort();
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                chfEmployee.Close();
            }
            return updateResult;
        }
    }
}

